/*
 * Copyright [2011] [Pettersen Consulting]
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.   
 */
package com.edb.os.xstream.writer;

import java.io.Writer;
import java.util.Arrays;
import java.util.List;

import com.edb.os.xstream.ClassWriterFactory;

/**
 * List handler
 * 
 * @author Oystein Pettersen
 * 
 */
public class ListWriter extends DefaultClassWriter {

    private static final List<String> IGNORED_ATTRIBUTE_NAMES = Arrays.asList("default", "int");
    
    public ListWriter(String name, Class<?> clazz, int depthIndex, int paramNameIndex, ClassWriterFactory classWriterFactory) {
        super(name, clazz, depthIndex, paramNameIndex, classWriterFactory);
    }

    @Override
    public void add(ClassWriter childAttribute, Writer writer) {
        // ensure that the list is not added again. Handled by list container writer.
        if(childAttribute.name().equalsIgnoreCase("list")) {
            return;
        }
        
        super.add(childAttribute, writer);
    }
    
    @Override
    public ClassWriter createChildWriter(String childName, int childDepthIndex) {
        // OK, we need to handle the list attribute here
        if(childName.equalsIgnoreCase("list")) {
            return new ListContainerWriter(childName, Void.class, childDepthIndex, this);
        }
        
        return super.createChildWriter(childName, childDepthIndex);
    }
    
    public class ListContainerWriter extends ClassWriter {

        private final ListWriter listWriter;

        public ListContainerWriter(String name, Class<?> clazz, int depthIndex, ListWriter listWriter) {
            super(name, clazz, depthIndex, listWriter.classWriterFactory);
            this.listWriter = listWriter;
        }
        
        @Override
        public void create(Writer writer) {
            // do nothing. It is already created
        }
        
        @Override
        public void add(ClassWriter childAttribute, Writer writer) {
            // unless we are ignoring
            if(IGNORED_ATTRIBUTE_NAMES.contains(childAttribute.name) == false) {
                
                // assume that this is a normal add method for the list
                try {
                    listWriter.clazz.getMethod("add", Object.class);
                    writer.write(String.format("%s.add(%s);\n", listWriter.paramName(), childAttribute.getValue()));
                } catch (Exception e) {
                    System.err.println("unable to add element for: " + childAttribute.name);
                }
            }
        }
    }
}
